package com.zz.yt.test.delegate.net.up;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.liys.view.LineProView;
import com.lxj.xpopup.XPopup;
import com.whf.android.jar.app.Latte;
import com.whf.android.jar.popup.LattePopupCenter;
import com.whf.android.jar.util.log.LatteLogger;
import com.zz.yt.test.R;

/**
 * 升级
 *
 * @author qf
 * @version 22.3.12
 */
class UpVersionPopup extends LattePopupCenter implements View.OnClickListener {

    private final static int MAX = 100;
    private boolean mIsDismiss = false;

    @NonNull
    static UpVersionPopup create(Context context) {
        return new UpVersionPopup(context);
    }

    private UpVersionPopup(Context context) {
        super(context);
    }

    @Override
    protected int setLayout() {
        return R.layout.popup_up_date;
    }


    @Override
    protected void initViews() {
        setOnClickListener(R.id.login_cancel, v -> {
            if (mIsDismiss) {
                ToastUtils.showShort("应用正在下载更新，暂时无法关闭");
                return;
            }
            dismiss();
        });
        setOnClickListener(R.id.login_confirm, this);
    }

    @Override
    public void onClick(View v) {
        mIsDismiss = true;
        LatteLogger.i("点击确定，开始更新");
        if (mClickDecisionListener != null) {
            mClickDecisionListener.onConfirm(null);
        }
    }

    /**
     * @param progress:下载进度
     */
    void setProgressBar(int progress) {
        LatteLogger.i("下载进度:" + progress);
        setShow(R.id.layout_versions, progress <= 0);
        setShow(R.id.layout_download, progress > 0);
        final LineProView proView = findViewById(R.id.id_line_pro_view);
        if (proView != null && progress > 0 && progress <= MAX) {
            proView.setProgress(progress);
            if (progress == MAX) {
                dismiss();
            }
        }
    }

    /**
     * 显示在中间
     */
    public final void start() {
        KeyboardUtils.hideSoftInput(Latte.getActivity());
        new XPopup.Builder(Latte.getActivity())
                .moveUpToKeyboard(false)
                .enableDrag(true)
                .isDestroyOnDismiss(false)
                .isThreeDrag(false)
                .keepScreenOn(true)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(this)
                .show();
    }
}
